package cn.ty.znsz.teacher;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Redis;

import cn.ty.znsz.common.model.SysArea;
import cn.ty.znsz.common.model.TeacherComment;
import cn.ty.znsz.common.model.TeacherInfo;
import cn.ty.znsz.common.utils.CommonUtils;

public class TeacherService {
	private TeacherInfo teacherDao=new TeacherInfo().dao();
	private SysArea areaDao=new SysArea().dao();
	private TeacherComment commentDao=new TeacherComment().dao();
	/**
	 * 首页分页
	 * @param paraToInt
	 * @param paraToInt2
	 * @param para
	 * @param para2
	 * @param para3
	 * @param para4
	 * @param para5
	 * @param para6
	 * @param para7
	 * @param para8
	 * @return
	 */
	public Page<TeacherInfo> paginate(Integer pageNumber, Integer pageSize, String name, String school, String major,
			String isCheck, String city, String county, String town, String village) {
		Page<TeacherInfo> page = null;
		String querySql = "SELECT * ";
		StringBuilder sql = new StringBuilder();
		sql.append("from teacher_info a WHERE 1=1 ");
		if(city != null && county == null){
			Record record=Db.findFirst("SELECT a.areaCode from sys_area a WHERE a.id=?;",city);
			sql.append(" and a.areaCode like '"+record.getStr("areaCode")+"%'");
		}
		if(county != null && town == null){
			Record record=Db.findFirst("SELECT a.areaCode from sys_area a WHERE a.id=?;",county);
			sql.append(" and a.areaCode like '"+record.getStr("areaCode")+"%'");
		}
		if(town != null && village == null){
			Record record=Db.findFirst("SELECT a.areaCode from sys_area a WHERE a.id=?;",town);
			sql.append(" and a.areaCode like '"+record.getStr("areaCode")+"%'");
		}
		if(village != null){
			Record record=Db.findFirst("SELECT a.areaCode from sys_area a WHERE a.id=?;",village);
			sql.append(" and a.areaCode like '"+record.getStr("areaCode")+"%'");
		}
		if(!"".equals(name) && name !=null){
			sql.append(" and a.name like '%"+name+"%'");
		}
		if(!"".equals(school) && school !=null){
			sql.append(" and a.school like '%"+school+"%'");
		}
		if(!"".equals(major) && major !=null){
			sql.append(" and a.major like '%"+major+"%'");
		}
		if(!"".equals(isCheck) && isCheck !=null){
			sql.append(" and a.isCheck = '"+isCheck+"'");
		}
		sql.append(" ORDER BY a.id");
		page = teacherDao.paginate(pageNumber, pageSize, querySql, sql.toString());		
		return page;
	}
	/**
	 * 保存师资
	 * @param data
	 * @param paraToInt
	 * @return
	 */
	public Ret teacherSave(TeacherInfo data, Integer areaId,String contentValue,String cookie) {
		String key=Redis.use().get(cookie);
		Date date=new Date();
		SysArea sysArea=areaDao.findById(areaId);
		data.setHeadChar(CommonUtils.getPinYinHeadChar(data.getName()));
		data.setAreaName(sysArea.getAreaName());
		data.setAreaCode(sysArea.getAreaCode());
		data.setCreateUserId(Redis.use().hget(key, "id"));
		data.setCreateUserName(Redis.use().hget(key, "userName"));
		data.setCreateAt(date);
		data.setIsCheck("未审");
		
		String str=CommonUtils.infoHtmlTept(data.getName(), Redis.use().hget(key, "userName"), contentValue);//生成html
        List<String> imgurls=CommonUtils.GetHtmlImageUrlList(str);//正则截取出html中的图片
        String category=new SimpleDateFormat("yyyyMM").format(date);
        String dir=PropKit.get("sharereal")+ "/upload/article/content/"+category;
        String fileName=StrKit.getRandomUUID()+".html";
        CommonUtils.strToHtml(fileName, str, dir);
		if(imgurls.size()>0){
			data.setImageUrl(PropKit.get("httpurl")+imgurls.get(0));
		}
		String contentUrl = PropKit.get("shareurl")+"/upload/article/content/"+category+"/"+fileName;
		data.setContentUrl(contentUrl);
		data.setContentValue(contentValue);
		data.use("zsql").save();
		return Ret.ok("操作成功");
		
	}
	/**
	 * 修改师资
	 * @param data
	 * @param paraToInt
	 * @param para
	 * @param paraToInt2
	 * @param cookie
	 * @return
	 */
	public Ret teacherUpdate(TeacherInfo data, Integer areaId, String contentValue, Integer id, String cookie) {
		String key=Redis.use().get(cookie);
		Date date=new Date();
		SysArea sysArea=areaDao.findById(areaId);
		data.setHeadChar(CommonUtils.getPinYinHeadChar(data.getName()));
		data.setAreaName(sysArea.getAreaName());
		data.setAreaCode(sysArea.getAreaCode());
		data.setCreateUserId(Redis.use().hget(key, "id"));
		data.setCreateUserName(Redis.use().hget(key, "userName"));
		data.setCreateAt(date);
		data.setIsCheck("未审");
		data.setId(id);
		String str=CommonUtils.infoHtmlTept(data.getName(), Redis.use().hget(key, "userName"), contentValue);//生成html
        List<String> imgurls=CommonUtils.GetHtmlImageUrlList(str);//正则截取出html中的图片
        String category=new SimpleDateFormat("yyyyMM").format(date);
        String dir=PropKit.get("sharereal")+ "/upload/article/content/"+category;
        String fileName=StrKit.getRandomUUID()+".html";
        CommonUtils.strToHtml(fileName, str, dir);
		if(imgurls.size()>0){
			data.setImageUrl(PropKit.get("httpurl")+imgurls.get(0));
		}
		String contentUrl = PropKit.get("shareurl")+"/upload/article/content/"+category+"/"+fileName;
		data.setContentUrl(contentUrl);
		data.setContentValue(contentValue);
		data.use("zsql").update();
		return Ret.ok("操作成功");
	}
	/**
	 * 删除师资
	 * @param paraToInt
	 * @return
	 */
	public Ret teacherDel(Integer id) {
		new TeacherInfo().use("zsql").deleteById(id);
		return Ret.ok("操作成功");
	}
	/**
	 * 审核师资
	 * @param paraToInt
	 * @param para
	 * @return
	 */
	public Ret teacherChecks(Integer id, String isCheck) {
		TeacherInfo data=new TeacherInfo();
		data.setId(id);
		data.setIsCheck(isCheck);
		data.use("zsql").update();
		return Ret.ok("操作成功");
	}
	/**
	 * 上传头像保存
	 * @param para
	 * @param file
	 * @param dir
	 * @return
	 */
	public Ret teacherUploadImg(int id, File file, String dir) {
		String filename = file.getName();
		if (filename.lastIndexOf('.') != -1) {
			filename = filename.substring(filename.lastIndexOf('.'));
		}
		filename =StrKit.getRandomUUID() + filename;
		String headImg = PropKit.get("shareurl") +dir+ "/"+ filename;
		file.renameTo(new File(PropKit.get("sharereal")+dir+"/"+filename));
		TeacherInfo data=new TeacherInfo();
		data.setId(id);
		data.setHeadImg(headImg);
		data.use("zsql").update();
		return Ret.ok("操作成功");
	}
	/**
	 * 评价分页
	 * @param paraToInt
	 * @param paraToInt2
	 * @param para
	 * @return
	 */
	public Page<TeacherComment> commentPaginate(Integer pageNumber, Integer pageSize, int id) {
		Page<TeacherComment> page = null;
		String querySql = "SELECT * ";
		StringBuilder sql = new StringBuilder();
		sql.append("from teacher_comment a WHERE a.teacherId="+id+" ");
		sql.append(" ORDER BY a.id");
		page = commentDao.paginate(pageNumber, pageSize, querySql, sql.toString());		
		return page;
	}
	/**
	 * 保存评价
	 * @param paraToInt
	 * @param para
	 * @return
	 */
	public Ret commentSave(Integer id, String commentValue,String cookie) {
		String key=Redis.use().get(cookie);
		TeacherComment data=new TeacherComment();
		data.setTeacherId(id);
		data.setCommentValue(commentValue);
		data.setCommentUserId(Redis.use().hget(key, "id"));
		data.setCreateAt(new Date());
		data.setIsCheck("未审");
		data.use("zsql").save();		
		return Ret.ok("操作成功");
	}
	/**
	 * 删除评价
	 * @param paraToInt
	 * @return
	 */
	public Ret commentDel(Integer id) {
		new TeacherComment().use("zsql").deleteById(id);
		return Ret.ok("操作成功");
	}
	/**
	 * 审核评价
	 * @param paraToInt
	 * @param para
	 * @return
	 */
	public Ret commentChecks(Integer id, String isCheck) {
		TeacherComment data=new TeacherComment();
		data.setId(id);
		data.setIsCheck(isCheck);
		data.use("zsql").update();
		return Ret.ok("操作成功");
	}
}
